#include "../mqserver/mq_binding.hpp"
#include <gtest/gtest.h>


mq::BindingManager::ptr bdm;
class BindingTest : public testing::Environment
{
public:
   virtual void SetUp() override
    {
        bdm = std::make_shared<mq::BindingManager>("./data/test.db");
    }

    virtual void TearDown() override
    {
        // bdm->clear();
    }
}; 

TEST(binding_test,insert_test)
{
    bdm->bind("exchange1","queue1","news.music.pop",true);
    bdm->bind("exchange1","queue2","news.music.#",true);
    bdm->bind("exchange2","queue1","news.sport.football",true);
    bdm->bind("exchange2","queue2","news.sport.#",true);
    ASSERT_EQ(bdm->size(),4);
}

TEST(binding_test,select_test)
{
   ASSERT_EQ(bdm->exists("exchange1","queue1"),true);
   ASSERT_EQ(bdm->exists("exchange1","queue2"),true);
   ASSERT_EQ(bdm->exists("exchange2","queue1"),true);
   ASSERT_EQ(bdm->exists("exchange2","queue2"),true);
   mq::Binding::ptr bp = bdm->getBinding("exchange2","queue1");
   ASSERT_NE(bp.get(),nullptr);
   ASSERT_EQ(bp->exchange_name,"exchange2");
   ASSERT_EQ(bp->msgqueue_name,"queue1");
   ASSERT_EQ(bp->binding_key,"news.sport.football");
}

TEST(binding_test,select_exchange_test)
{
    mq::MsgQueueBindingMap mqbm = bdm->getExchangeBindings("exchange1");
    ASSERT_EQ(mqbm.size(),2);
    ASSERT_NE(mqbm.find("queue1"),mqbm.end());
    ASSERT_NE(mqbm.find("queue2"),mqbm.end());
}

TEST(binding_test,remove_exchange_test)
{
    bdm->removeExchangeBindngs("exchange1");
    ASSERT_EQ(bdm->exists("exchange1","queue1"),false);
    ASSERT_EQ(bdm->exists("exchange1","queue2"),false);
    ASSERT_EQ(bdm->exists("exchange2","queue1"),true);
    ASSERT_EQ(bdm->exists("exchange2","queue2"),true);
}

// TEST(binding_test,remove_msgqueue_test)
// {
//     bdm->removeMsgQueueBindings("queue1");
//     ASSERT_EQ(bdm->exists("exchange1","queue1"),false);
//     ASSERT_EQ(bdm->exists("exchange1","queue2"),false);
//     ASSERT_EQ(bdm->exists("exchange2","queue1"),false);
//     ASSERT_EQ(bdm->exists("exchange2","queue2"),true);
// }

TEST(binding_test,remove_signal_test)
{
    bdm->unbind("exchange2","queue2");
    ASSERT_EQ(bdm->exists("exchange2","queue2"),false);
    ASSERT_EQ(bdm->exists("exchange2","queue1"),true);
}

int main(int argc,char *argv[])
{
    testing::AddGlobalTestEnvironment(new BindingTest);
    testing::InitGoogleTest(&argc,argv);
    RUN_ALL_TESTS();
    return 0;
}